﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class DocumentController : AsyncCommonController
    {
        public DocumentController() { }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Outgoing(string search)
        {
            var data = EdexContext.GetOutgoingInvoices(search);
            return View(data);
        }

        public ActionResult OutgoingInvoiceEdit(Guid id)
        {
            var data = EdexContext.GetOutgoingInvoice(id.ToString());
            return View(data);
        }

        public ActionResult OutgoingInvoiceDetailsEdit(Guid id)
        {
            var data = EdexContext.GetOutgoingInvoiceLine(id.ToString());
            return View(data);
        }

        public ActionResult OutgoingInvoiceNew()
        {
            return View();
        }

        public ActionResult OutgoingInvoiceDetailsNew(Guid id)
        {
            var data = new EdexApiClient.InvoiceLines
            {
                INVOICE_GUID = id
            };

            return View(data);
        }

        public ActionResult Incoming(string search)
        {
            var data = EdexContext.GetIncomingInvoices(search);
            return View(data);
        }

        public ActionResult Details(Guid id)
        {
            var data = EdexContext.GetIncomingInvoice(id.ToString());
            return View(data);
        }

        public ActionResult LineDetails(Guid id)
        {
            var data = EdexContext.GetIncomingInvoiceLine(id.ToString());
            return View(data);
        }

        [HttpPost]
        public ActionResult SaveOutgoingInvoice(EdexApiClient.Invoices invoice)
        {
            if (invoice.GUID_RECORD != Guid.Empty && invoice.GUID_RECORD != null)
                EdexContext.UpdateInvoice(invoice);
            else
                EdexContext.AddInvoice(invoice);
            
            return Content("OK");
        }

        [HttpPost]
        public ActionResult SaveOutgoingInvoiceLine(EdexApiClient.InvoiceLines line)
        {
            if (line.GUID_RECORD != Guid.Empty && line.GUID_RECORD != null)
                EdexContext.UpdateInvoiceDetails(line);
            else
                EdexContext.AddInvoiceDetails(line);

            return Content("OK");
        }

        [HttpPost]
        public ActionResult DeleteOutgoingInvoice(string id)
        {
            EdexContext.DeleteInvoice(id);
            return Content("OK");
        }

        [HttpPost]
        public ActionResult DeleteOutgoingInvoiceLine(string id)
        {
            EdexContext.DeleteInvoiceDetails(id);
            return Content("OK");
        }


        [HttpGet]
        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        [ActionName("Upload")]
        public async Task<ActionResult> UploadFile(CancellationToken cancellationToken)
        {
            for (var i = 0; i < Request.Files.Count; i++)
            {
                var result = await EdexContext.UploadOutgoingInvoices(Request.Files[i].InputStream, Request.Files[i].FileName);

                if (result.StatusCode != System.Net.HttpStatusCode.OK &&
                    result.StatusCode != System.Net.HttpStatusCode.NoContent)
                {
                    var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                    var data = js.Deserialize<EdexApiClient.ExceptionInfo>(result.Content.ReadAsStringAsync().Result);

                    throw new Exception(data.ExceptionMessage);
                }
            }

            return View("UploadComplete");
        }

        public ActionResult Download(Guid[] GUID_RECORD)
        {
            var data = EdexContext.GetIncomingInvoicesCommerceML(GUID_RECORD);
            return File(data, "application/xml", "commerceML.xml");
        }

    }
}
